import pygame 
import sys

from Settings import *
from Scene import *
from Sprites import *
from Merchant import Shop

pygame.init()
g_window = pygame.display.set_mode((1010, 600))  # 窗口大小
def gameloop():
    pygame.mixer_music.load(r".\assets\audio\main.ogg")
    pygame.mixer_music.play(-1)
    hit_sound=pygame.mixer.Sound(r".\assets\audio\hit.wav")
    Boss_hit_sound=pygame.mixer.Sound(r".\assets\audio\monster_get_attack.mp3")
    Boss_hit_sound.set_volume(5)
    hit_sound.set_volume(5)
    dead_music=pygame.mixer.Sound(r".\assets\audio\death.wav")
    dead_music.set_volume(5)
    music_timer=0
    if __name__ == "__main__":
        scene = SceneLike()
        for wall in scene.walls:
            wall_sprite.add(wall)
        player1 = Player()
        # 初始化商人 - 确保 player1 是第一个参数
        merchant = Shop(player1, g_window, position=pygame.math.Vector2(400, 400))
        
        clock = pygame.time.Clock()
        total_map = [scene.tiles,scene.walls]
        for tile in total_map[0]:
                tile.draw(scene.carema)
        for wall in total_map[1]:
                wall.draw(scene.carema)    
        monsters = gen_monster()
        monster_group=pygame.sprite.Group()
        Boss=eye_beast((3400,150))
        index=-1
        for monster in monsters:
                index+=1
                monster_group.add(monster)
        attack_old_time=0
        dead_index=[]
        dead_index.append(index)
        while True:
            clock.tick(30)
            dt=clock.get_time()/1000
            g_window.fill((0, 0, 0))  # 全屏涂黑
            scene_update = Scene(player1)
            player1.draw(g_window,scene_update.camera) 
            if(Boss.health>0):
                Boss.draw(g_window,scene_update.camera)
            for monster in monsters:
                if(monster.index not in dead_index):
                    #print(f"my index is{monster.index}")
                    monster.draw(g_window,scene_update.camera)
            for tile in total_map[0]:
                tile.draw(scene_update.camera)
            for wall in total_map[1]:
                wall.draw(scene_update.camera)    
        
             # 计算商人相对于相机的位置
            merchant_screen_pos = merchant.position - scene_update.camera
            # 绘制商人角色
            g_window.blit(merchant.npc_image, (merchant_screen_pos.x, merchant_screen_pos.y))
            
            for event in pygame.event.get():  # 将pygame默认事件如键盘等转换到自己的队列中
           
                if event.type == pygame.QUIT:  # 退出事件
                    pygame.quit()
                    sys.exit()
            player1.update(dt=dt,camera = scene_update.camera)
            #print(player1.health)
            for monster in monsters:
                if(monster.index not in dead_index):
                    monster.update(player1, dt = dt, camera = scene_update.camera)
            if(Boss.health>0):
                Boss.update(player1,dt,camera=scene_update.camera)
            if player1.sword:
                now=pygame.time.get_ticks()
                if now-attack_old_time>=300:
                    attack_old_time=now
                    collision_sprites=pygame.sprite.spritecollide(player1.sword,monster_group,False)
                    if collision_sprites:
                        for target_sprite in collision_sprites:
                            if(target_sprite.index not in dead_index):
                                hit_sound.play()
                                #print(f"yes you attack monster{target_sprite.index},{target_sprite.health}-{player1.attack_damage}")
                                if target_sprite.health-player1.attack_damage<=0:
                                    dead_music.play()
                                    player1.gold+=2
                                    #print(f"delete{target_sprite.index}")
                                    dead_index.append(target_sprite.index)
                                    monster_group.remove(monster)
                                target_sprite.get_damage(player1)
                    if pygame.sprite.collide_rect(player1.sword,Boss):
                            if(Boss.health<=0):
                                 Boss.kill()
                                 pass

                            else:
                                 current_time=pygame.time.get_ticks()
                                 if(current_time-music_timer>=2000):
                                    Boss_hit_sound.play()
                                    music_timer=current_time
                                 Boss.get_damage(player1)
            gold = pygame.transform.scale(pygame.image.load(GamePath.gold),(40,40))
            g_window.blit(gold,(850,10))    
            R_C = pygame.transform.scale(pygame.image.load(GamePath.R_C),(40,40))
            g_window.blit(R_C,(900,10))
            font = pygame.font.Font(GamePath.font,30)
            text = font.render(f"{player1.gold}",True,(255,255,255))
            g_window.blit(text,(950,10))
            heart = pygame.transform.scale(pygame.image.load(GamePath.R_C_1),(40,40))
            g_window.blit(heart,(10,10))
            g_window.blit(R_C,(60,10))
            text = font.render(f"{player1.health}",True,(255,255,255))
            text_2 = font.render(f"{player1.health}",True,(255,255,255))
            g_window.blit(text,(120,10))
            if player1.health<=0:
                tupian()
                font = pygame.font.Font(GamePath.font,30)
                text = font.render("你输了，游戏结束，请退出重试",True,(255,255,255))
                g_window.blit(text,(500,350))
                pygame.display.update() 
                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            pygame.quit()
                            sys.exit()
            if Boss.health<=0:
                 tupian()
                 font = pygame.font.Font(GamePath.font,30)
                 text = font.render("你赢了，游戏结束",True,(255,255,255))
                 g_window.blit(text,(500,350))  
                           
                       
            pygame.display.flip()  # 缓冲绘制到屏幕上
def tupian():
     menu_image =  pygame.transform.scale(pygame.image.load(GamePath.menu),(1010,600))
     g_window.blit(menu_image,(0,0))
def music():
     pygame.mixer.init()
     pygame.mixer_music.load(GamePath.origin_song)
     pygame.mixer.music.set_volume(1)   
     pygame.mixer.music.play()    
def ziti():
    font=pygame.font.Font(GamePath.font,30)#加载字体
    text=font.render("魔法少女大冒险",True,(255,255,255))
    text_2=font.render("点击任意处开始游戏",True,(255,255,255))
    g_window.blit(text,(509,400))
    g_window.blit(text_2,(480,450))
    pygame.display.update()
running = True
running_2 = False
if __name__ == "__main__":
    tupian()
    ziti()
    music()
    i=0
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                mx,my=pygame.mouse.get_pos()
                i+=1
                if 0<=mx<=1000 and 0<=my<=800 and i==1: 
                    running=False
                    running_2=True                                    
                    pygame.display.update() 
    while running_2:
        gameloop()        